package controller

import (
	"sugar/database"
	"sugar/errorcode"
	"sugar/response"

	"gopkg.in/guregu/null.v3"
)

func (op *Operation) checkDatasetAuth(userID string, datasetID int64, auth int) (bool, error) {
	return true, nil
}

//
func (op *Operation) ListDataset(userID string, pageSize int, pageNo int) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	datasets, err := op.DbOp.ListDatasetByUser(userID, pageSize, pageNo)
	if err != nil {
		panic(errorcode.ListDatasetError)
	}
	return response.NewSuccess(datasets)
}

//
func (op *Operation) GetDataset(userID string, datasetID int64) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	dataset, err := op.DbOp.GetDatasetByID(datasetID)
	op.checkDatasetAuth(userID, datasetID, 0)
	if err != nil {
		panic(errorcode.GetDatasetError)
	}
	return response.NewSuccess(dataset)
}

//
func (op *Operation) CreateDataset(userID string, datasetName string, datasetType int, datasetObject string, datasetObjectType int, token null.String, description null.String, public int) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	if exist, _ := op.DbOp.IsDatasetExist(userID, datasetName); exist {
		panic(errorcode.DatasetAlreadyExistError)
	}
	datasetID, err := op.DbOp.CreateDataset(userID, datasetName, datasetType, datasetObject, datasetObjectType, token, description, public)
	if err != nil {
		panic(errorcode.CreateDatasetError)
	}
	res := make(map[string]interface{})
	res["id"] = datasetID
	return response.NewSuccess(res)

}

func (op *Operation) checkDatasetAvailable(userID string, datasetID int64) (*database.Dataset, error) {
	dataset, err := op.DbOp.GetDatasetByID(datasetID)
	if err != nil {
		panic(errorcode.GetDatasetError)
	}
	op.checkDatasetAuth(userID, datasetID, 0)
	return dataset, nil
}
